package org.bipt.gps;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetStreetByHttp {

	public static String getStreet(String lat,String lng) throws ClientProtocolException, IOException{
	String place="";   
	HttpPost post = new HttpPost( "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=false&language=Zh-cn");     
	    DefaultHttpClient httpclient = new DefaultHttpClient();
	    HttpResponse response = httpclient.execute(post);
	    HttpEntity entity = response.getEntity();
	    String str = EntityUtils.toString(entity);    
	    int status_code = response.getStatusLine().getStatusCode();
	    if (status_code == 200) {
	    
	     try { 
	      JSONArray  jArr = new JSONArray();
	      JSONObject jb = new JSONObject(str);
	      //String result = jb.getString("result");
	      jArr = jb.getJSONArray("results");
	      
	      place=((JSONObject)jArr.get(0)).get("formatted_address").toString();
	     } catch (JSONException e) {
	      // TODO Auto-generated catch block    
	      e.printStackTrace();
	     }
	    }
	   return place;
	}
}
